package com.concerneyes.health.healthView;

import android.content.Context;
import android.graphics.Color;

import com.concerneyes.R;
import com.concerneyes.health.Task.GlassManDataImpl;
import com.concerneyes.health.Task.GlassManDataListener;
import com.concerneyes.health.model.HealthDataModel;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.Legend.LegendForm;
import com.github.mikephil.charting.components.Legend.LegendPosition;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import common.AbsBaseView;
import util.ActivityUtil;
import util.Util;

/**
 * Created by Administrator on 2017/6/29 0029.
 */

public class HisDataAnalysisView extends AbsBaseView implements GlassManDataListener {
    private LineChart chartRight, chartLeft;
    private Context context;
    private String manId;
    private GlassManDataImpl dataImpl;

    public HisDataAnalysisView(Context context, String manId) {
        super(context);
        this.context = context;
        this.manId = manId;
        initView();

    }

    private void initView() {
        chartRight = (LineChart) findViewById(R.id.chart_right);
        chartLeft = (LineChart) findViewById(R.id.chart_left);
        chartRight.zoom(1.0f, 1.0f, 0f, 1f);
        chartLeft.zoom(1.0f, 1.0f, 0f, 1f);
        chartRight.setDescription("");
        chartLeft.setDescription("");
        dataImpl = new GlassManDataImpl(getActivity(), this);
        dataImpl.listGlassManData(manId);

    }

    private void setEyesData(List<HealthDataModel> model) {
        Collections.reverse(model);
        ArrayList<String> xVals = new ArrayList<String>();
        for (int i = 0; i < model.size(); i++) {
            xVals.add(Util.changeStringDataStyle(model.get(i).getDataTime(),"yyyy.MM"));
        }
        ArrayList<Entry> yLeftVals = new ArrayList<Entry>();
        for (int i = 0; i < model.size(); i++) {
            yLeftVals.add(new Entry(Float.parseFloat(model.get(i).getFarLeftDegree()), i));
        }
        ArrayList<Entry> yRightVals = new ArrayList<Entry>();
        for (int i = 0; i < model.size(); i++) {
            yRightVals.add(new Entry(Float.parseFloat(model.get(i).getFarRightDegree()), i));
        }
        initDoubleLineChart(getContext(), chartRight, xVals, yRightVals, "");
        initDoubleLineChart(getContext(), chartLeft, xVals, yLeftVals, "");
    }

    @Override
    public int layoutResId() {
        return R.layout.view_his_analysis;
    }

    @Override
    public void loadData(Object data) {

    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    /**
     * @param context    上下文
     * @param mLineChart 折线图控件
     * @param xValues    折线在x轴的值
     * @param yValue     折线在y轴的值
     */
    public void initDoubleLineChart(Context context, LineChart mLineChart,
                                    ArrayList<String> xValues, List<Entry> yValue, String lineName) {

        initDataStyle(context, mLineChart);

        LineDataSet dataSet = new LineDataSet(yValue, lineName);
        //折线的颜色
        dataSet.setColor(context.getResources().getColor(R.color.chat_line));
        dataSet.setLineWidth(2);
        //折线上圆球的颜色
        dataSet.setDrawCircles(false);
        dataSet.setCircleColor(Color.GRAY);
        dataSet.setDrawCubic(true);
        dataSet.setCubicIntensity(0.2f);
        dataSet.setDrawFilled(true);
        dataSet.setFillDrawable(context.getResources().getDrawable(R.drawable.shade_chat));
        dataSet.setDrawValues(true);
        dataSet.setHighLightColor(Color.TRANSPARENT);
        dataSet.setValueTextColor(Color.WHITE);
        // 构建一个类型为LineDataSet的ArrayList 用来存放所有 y的LineDataSet
        // 他是构建最终加入LineChart数据集所需要的参数
        ArrayList<ILineDataSet> dataSets = new ArrayList<ILineDataSet>();
        // 将数据加入dataSets
        dataSets.add(dataSet);
        // dataSets.add(dataSet1);
        // 构建一个LineData 将dataSets放入
        LineData lineData = new LineData(xValues, dataSets);
        // 将数据插入
        mLineChart.setData(lineData);
        // 设置动画效果
        mLineChart.animateXY(2000, 2000);
        mLineChart.invalidate();
    }

    /**
     * @param context
     * @param mLineChart
     * @Description:初始化图表的样式
     */
    private void initDataStyle(Context context, LineChart mLineChart) {
        // 设置图表是否支持触控操作
        // mLineChart.setTouchEnabled(true);
        mLineChart.setScaleEnabled(true);
        // 设置点击折线点时，显示其数值
        // MyMakerView mv = new MyMakerView(context, R.layout.item_mark_layout);
        // mLineChart.setMarkerView(mv);
        // 设置折线的描述的样式（默认在图表的左下角）
        Legend title = mLineChart.getLegend();
        title.setForm(Legend.LegendForm.LINE);
        // 设置x轴的样式
        XAxis xAxis = mLineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisLineColor(Color.BLACK);
        xAxis.setAxisLineWidth(0);
        xAxis.setDrawAxisLine(true);
        xAxis.setEnabled(true);
        xAxis.setDrawGridLines(false);
        xAxis.setTextColor(Color.GRAY);
        xAxis.setGridColor(Color.GRAY);
        // 设置左边y轴的样式
        YAxis yAxisLeft = mLineChart.getAxisLeft();
        yAxisLeft.setAxisLineColor(Color.parseColor("#ffffff"));
        yAxisLeft.setLabelCount(3, false);
        yAxisLeft.setAxisLineWidth(1);
        yAxisLeft.setDrawGridLines(false);
        yAxisLeft.setDrawAxisLine(true);
        yAxisLeft.setAxisLineColor(Color.BLACK);
        yAxisLeft.setAxisLineWidth(0);
        yAxisLeft.setEnabled(true);
        yAxisLeft.setTextColor(Color.GRAY);
        //数据倒置
        yAxisLeft.setInverted(true);
        // 设置右边y轴的样式
        YAxis yAxisRight = mLineChart.getAxisRight();
        yAxisRight.setEnabled(false);

        Legend mLegend = mLineChart.getLegend();
        mLegend.setPosition(LegendPosition.BELOW_CHART_LEFT);
        mLegend.setForm(LegendForm.LINE);
        mLegend.setFormSize(10f);
        mLegend.setTextSize(8f);
        mLegend.setTextColor(0x88ffffff);
        mLegend.setXEntrySpace(5f);

    }

    @Override
    public void glassManDataListSuccess(List<HealthDataModel> model) {
        setEyesData(model);
    }

    @Override
    public void glassManDataListFail() {

    }

    @Override
    public void deleteGlassManDataSuccess(List<HealthDataModel> model) {

    }

    @Override
    public void deleteGlassManDataFail() {

    }

    @Override
    public void addGlassManDataSuccess() {

    }

    @Override
    public void addGlassManDataFail() {

    }
}
